package com.hq.ims.data.service;

import com.hq.ims.data.config.verification.VdAdd;
import com.hq.ims.data.dao.CompanyServiceAreaHistoryMapper;
import com.hq.ims.data.entity.Area;
import com.hq.ims.data.entity.CompanyServiceAreaHistory;
import com.hq.ims.data.entityenum.EnumDataType;
import com.hq.ims.data.vo.CompanyServiceAreaMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.utils.util.ObjectUtils;
import com.hq.utils.util.StringUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author jiang bai ping
 * @Email 526666031@qq.com
 * @DateTime 2024-10-29 14:44:52
 * @Detail 企业服务区域服务实现类
 */
@Service
public class BaseCompanyServiceAreaHistoryService extends BaseService<CompanyServiceAreaHistoryMapper, CompanyServiceAreaHistory> {

    @Resource
    private BaseAreaService baseAreaService;

    public List<CompanyServiceAreaHistory> getCompanyAllServiceAreaList(String id, Integer dataType) {
        return this.lambdaQuery().eq(CompanyServiceAreaHistory::getCompanyCheckId, id)
                .eq(CompanyServiceAreaHistory::getDataType, dataType)
                .list();
    }


    @Transactional(rollbackFor = Exception.class)
    public CompanyServiceAreaHistory addData(CompanyServiceAreaMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdAdd.class);

        //账值新的对象
        CompanyServiceAreaHistory entity = new CompanyServiceAreaHistory();

        Area provinceArea = baseAreaService.getById(vo.getProvinceCode());
        ObjectUtils.isNullThrow(provinceArea, "省不存在");

        if (StringUtils.isNotEmpty(vo.getCityCode())){
            Area cityArea = baseAreaService.getById(vo.getCityCode());
            entity.setCityCode(cityArea.getAreaId());
            entity.setCityName(cityArea.getAreaName());
        }

        entity.setCompanyCheckId(vo.getCheckId());
        entity.setCompanyId(vo.getCompanyId());
        entity.setProvinceCode(provinceArea.getAreaId());
        entity.setProvinceName(provinceArea.getAreaName());
        entity.setDataType(EnumDataType.After);

        entity.insert();
        return entity;
    }

    @Transactional(rollbackFor = Exception.class)
    public void delByCompanyId(String companyId) {
        this.lambdaUpdate().eq(CompanyServiceAreaHistory::getCompanyId,companyId).remove();
    }
}
